package com.club.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.club.common.constant.Constant;
import com.club.entity.PageBean;
import com.club.entity.R;
import com.club.entity.SysUser;
import com.club.mapper.UserMapper;
import com.club.service.UserService;
import com.club.util.DateUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhangfc
 * @date 2022/12/16 15:20
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, SysUser> implements UserService {


    /**
     * 分页查询用户信息
     *
     * @param pageBean 分页bean
     * @return R
     */
    @Override
    public R listUser(PageBean pageBean) {

        //搜索的内容
        String query = pageBean.getQuery().trim();

        Page<SysUser> userPage = page(new Page<>(pageBean.getPageNum(), pageBean.getPageSize()), new QueryWrapper<SysUser>().like(StrUtil.isNotEmpty(query), "stuid", query));

        List<SysUser> userList = userPage.getRecords();
        List<Map<String, Object>> userMapList = userList.stream()
                .map(user -> {
                    Map<String, Object> userMap = BeanUtil.beanToMap(user);
                    userMap.put("birthday", DateUtil.formatDate(user.getBirthday(), Constant.TimePlate.STAND_TIME));
                    return userMap;
                }).collect(Collectors.toList());

        Map<String, Object> responseMap = MapUtil.builder(new HashMap<String, Object>())
                .put("total", userPage.getTotal())
                .put("userList", userMapList).build();
        return R.ok(responseMap);
    }
}
